#!/usr/bin/env python

import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api.labs import taskqueue
from schema import *

import logging

class DocumentLinkCollater(webapp.RequestHandler):
  def post(self):
    PAGESIZE = 5
    next = None
    computationid = long(self.request.get("computationid"))
    logging.debug("This is for computation %i." % computationid)
    key_name = self.request.get("docid")
    logging.debug("I'm going to collate the links for document %s" % key_name)
    docid = db.ByteString(checksum_from_key_name(key_name))
    node_key = self.request.get("node_key")
    
    doc_nodes = LinkGraphNode.all().order("__key__").filter('docid =', docid)
    nodes = doc_nodes.filter('computationid =', computationid)
    if node_key:
      logging.debug("This is a continuation.")
      logging.debug("I'm starting from node %s" % node_key)
      key = db.Key(node_key)
      batch_of_nodes = nodes.filter('__key__ >=', key).fetch(PAGESIZE+1)
    else:
      logging.debug("This the first batch.")
      batch_of_nodes = nodes.fetch(PAGESIZE+1)

    logging.debug("There are %i nodes in this batch." % len(batch_of_nodes))

    final_node = batch_of_nodes[-1]
    links = ""
    for node in batch_of_nodes[:-1]:
      links += node.links
    final_node.links += links
    logging.debug("The final node has %i bytes." % len(final_node.links))
    final_node.put()

    if len(batch_of_nodes) == PAGESIZE+1:
      logging.debug("I'm going to continue this at a later time.")
      next = batch_of_nodes[-1]
      logging.debug("The next batch will start with %s." % str(next.key()))
      task = taskqueue.Task(
        url='/collate-document-links',
        params={
          'computationid': computationid,
          'docid': key_name_from_checksum(docid),
          'node_key': str(next.key()),
        }
      )
      task.add(queue_name='parse')

    batch_of_nodes.pop()
    logging.debug("Deleting batch of nodes")
    db.delete(batch_of_nodes)


def main():
  logging.getLogger().setLevel(logging.DEBUG)
  wsgiref.handlers.CGIHandler().run(webapp.WSGIApplication([
    ('/collate-document-links', DocumentLinkCollater),
  ]))


if __name__ == '__main__':
  main()
